mod response;
mod domain;
mod controller;
mod util;

#[macro_use]
extern crate rbatis;

#[macro_use]
extern crate lazy_static;
use rbatis::rbatis::Rbatis;
use std::sync::Arc;
use rbdc_mysql::driver::MysqlDriver;
use jwt_simple::prelude::HS256Key;


use rocket::{ catchers, routes, Request,catch};
use rocket::fairing::AdHoc;
use rocket::serde::json::{Value,serde_json::json};
use response::response_object::RespData;

lazy_static! {
     // Rbatis是线程、协程安全的，运行时的方法是Send+Sync，无需担心线程竞争
    static ref RB:Rbatis = Rbatis::new();

    static ref KEY: HS256Key = HS256Key::generate();
}


#[catch(404)]
fn not_found(_req: &Request) -> Value {
    json!(RespData {
        code: 404,
        msg: "未找到相关内容...",
        data: ()
    })
}
#[rocket::main]
async fn main()-> Result<(), Box<dyn std::error::Error>>{
    fast_log::init(fast_log::Config::new().console()).expect("rbatis init fail");
    // let url = env::var("DATABASE_URL")
    //     .expect("no config env exist");
    ///连接数据库

    RB.init(MysqlDriver{},"mysql://***:***@43.140.193.162:3306/wxdata")
        .unwrap();
    let rb = Arc::new(&RB);
    let _ = rocket::build()
        .register("/", catchers![not_found])
        .mount(
            "/",
            routes![
                controller::GoodsController::get_goods_info,
                controller::user_shopping::add_to_shop_cart,
                controller::user_shopping::change_good_num,
                controller::user_shopping::get_user_cart,
                controller::user_shopping::delete_good,
                controller::order_controller::get_all_order,
                controller::user_shopping::create_order,
            ],
        )
        .attach(AdHoc::on_ignite("Rbatis Database", |rocket| async move {
            rocket.manage(rb)
        }))
        .launch()
        .await?;
    Ok(())
}
